const Controller = require('egg').Controller;

class UserController extends Controller {


    //pc端login
    async login () {
        console.log('login');

        const { ctx, app, logger } = this;

        //判断是否已经登录
        const sessionid = ctx.cookies.get('juyou.session.id');
        const redisSessions = await app.redis.get('member').hgetall('juyou_session_');
        const sessionV = redisSessions[sessionid];

        // ctx.logger.info('sessionid : ' + sessionid);
        // ctx.logger.info('redisSessions : ' + redisSessions);
        // ctx.logger.info('sessionV : ' + sessionV);

        if (sessionV !== undefined) {
            ctx.helper.success(sessionid);
            return;
        }

        //判断用户名密码
        const username = ctx.query.username;
        const password = ctx.query.password;

        if(username == undefined || password == undefined){
            ctx.helper.error('请输入用户名密码。');
            return;
        }


        const userInfo = await ctx.service.user.login(username, password);

        //ctx.logger.info(userInfo);

        console.log(userInfo);

        if(userInfo == undefined){
            ctx.helper.error(1004, '用户名密码错误');
            return;
        }

        const user = await ctx.service.user.user(userInfo.id);

        //ctx.logger.info(user);

        const step = await ctx.service.user.step(user.company_code);

        //ctx.logger.info(step);

        const companyInfo = await ctx.service.company.info(user.company_code);

        //ctx.logger.info(companyInfo);

        //登录成功后续操作
        const uuid = await ctx.service.util.uuid(40, 16);
        // 客户端的cookie
        ctx.cookies.set('juyou.session.id', uuid);
        
        const auth = await ctx.service.user.auth(userInfo.id);

        const sessionValue = {
            '_user' : {
                ...user, 
                'bind_seller_company_code' : user.company_code,
            },
            '_auth' : auth,
            '_wxaccount' : companyInfo == undefined ? {} : companyInfo,
        };

        console.log(sessionValue);

        // 账号过期时间
        await app.redis.get('member').hset('juyou_session_', uuid, user.company_code + '|43200000|' + (new Date().getTime()));
        //await app.redis.get('system').expire('juyou_session_' + uuid, 24 * 3600);

        //logger.debug('juyou_session_' + uuid + '_info');

        await app.redis.get('member').set('juyou_session_' + uuid + '_info', JSON.stringify(sessionValue));
        await app.redis.get('member').expire('juyou_session_' + uuid + '_info', 24 * 3600);

        // 设置响应体和状态码

        // ctx.helper.success({
        //     uuid,
        //     step,
        //     userid : userInfo.id,
        // });

        ctx.helper.success(uuid);
        
    }



    async logout () {
        const { app, ctx } = this;

        const uuid = ctx.cookies.get('juyou.session.id');

        ctx.cookies.set('juyou.session.id', null);

        await app.redis.get('member').hdel('juyou_session_', uuid);

        await app.redis.get('member').del('juyou_session_' + uuid + '_info');

        ctx.body = {
          data : 'ok',
          errcode : 0,
        };
        ctx.status = 201;

    }


    async info () {
        const { app, ctx } = this;

        ctx.helper.success({
            user : ctx._user,
            account : ctx._wxaccount,
        });
    }

    //0,可以注册。1，不可以注册
    async getInfoByMobile () {
        const { ctx } = this;

        const mobile = ctx.request.body.mobile;

        const result = await ctx.service.user.getInfoByMobile(mobile);

        const r = result.id == undefined ? '0' : '1';

        ctx.helper.success(r);
    }

    async getStep () {
        const { ctx } = this;

        const result = await ctx.service.user.step(ctx._user.company_code);

        ctx.helper.success(result);
    }

    //0:密码不对，1:密码正确
    async checkPass () {
        const { ctx } = this;

        const { mobile, password } = ctx.request.body;

        const result = await ctx.service.user.login(mobile, password);

        const ret = result == undefined ? '0' : '1';

        ctx.helper.success(ret);

    }

    //修改密码，0，失败，1，成功
    async modifyPass () {
        const { ctx } = this;

        const { mobile, password, oldpassword } = ctx.request.body;

        const result = await ctx.service.user.modifyPass(mobile, password, oldpassword);

        const ret = result == false ? '0' : '1';

        ctx.helper.success(ret);
    }
    
}
module.exports = UserController;